
package com.hlkj.pay.service.merchant.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.LocalContext;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.framework.mybatis.core.query.QueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.common.constants.RedisConstants;
import com.hlkj.pay.dto.merchant.config.MerchantPaymentAccountConfigPageQueryDto;
import com.hlkj.pay.dto.merchant.config.MerchantPaymentConfigMappingDto;
import com.hlkj.pay.dto.merchant.config.MerchantPaymentConfigMappingPageQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.mapper.merchant.MerchantConfigMapper;
import com.hlkj.pay.infrastructure.mapper.merchant.MerchantPaymentAccountConfigMapper;
import com.hlkj.pay.infrastructure.mapper.merchant.MerchantPaymentConfigMappingMapper;
import com.hlkj.pay.infrastructure.model.merchant.MerchantConfigDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantPaymentAccountConfigDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantPaymentConfigMappingDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.merchant.IMerchantConfigService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/09/12 13:43
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MerchantConfigServiceImpl extends CommonSnFilterService implements IMerchantConfigService {

    private final MerchantConfigMapper merchantConfigMapper;

    private final MerchantPaymentAccountConfigMapper merchantPaymentAccountConfigMapper;

    private final MerchantPaymentConfigMappingMapper merchantPaymentConfigMappingMapper;

    @Override
    public MerchantConfigDO queryConfig(String sn) {
        return merchantConfigMapper.selectOne(MerchantConfigDO::getSn, sn);
    }

    @Override
    public List<MerchantConfigDO> queryConfig(List<String> snList) {
        LambdaQueryWrapperX<MerchantConfigDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.inIfPresent(MerchantConfigDO::getSn, snList);
        return merchantConfigMapper.selectList(queryWrapper);
    }

    @Override
    public void saveConfig(MerchantConfigDO merchantConfigDO) {
        merchantConfigMapper.insert(merchantConfigDO);
    }

    @Override
    @CacheEvict(value = RedisConstants.MERCHANT_CONFIG, key = "#merchantConfigDO.sn")
    public void updateConfig(MerchantConfigDO merchantConfigDO) {
        merchantConfigMapper.updateById(merchantConfigDO);
    }

    @Override
    @CacheEvict(value = RedisConstants.MERCHANT_CONFIG, key = "#sn")
    public void delConfig(String sn) {
        LambdaQueryWrapperX<MerchantConfigDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(MerchantConfigDO::getSn, sn);
        merchantConfigMapper.delete(queryWrapper);
    }

    @Override
    public MerchantPaymentAccountConfigDO queryPaymentAccountConfig(String cardNo) {
        MerchantPaymentAccountConfigPageQueryDto configPageQueryDto = new MerchantPaymentAccountConfigPageQueryDto();
        configPageQueryDto.setCardNo(cardNo);
        configPageQueryDto.setSn(LocalContext.get().getSn());
        LambdaQueryWrapperX<MerchantPaymentAccountConfigDO> queryWrapperX = buildPaymentAccountWrapper(configPageQueryDto);
        queryWrapperX.last("limit 1");
        return merchantPaymentAccountConfigMapper.selectOne(queryWrapperX);
    }

    @Override
    public void addPaymentAccountConfig(MerchantPaymentAccountConfigDO merchantPaymentAccountConfigDO) {
        merchantPaymentAccountConfigMapper.insert(merchantPaymentAccountConfigDO);
    }

    @Override
    public void updatePaymentAccountConfig(MerchantPaymentAccountConfigDO merchantPaymentAccountConfigDO) {
        merchantPaymentAccountConfigMapper.updateById(merchantPaymentAccountConfigDO);
    }

    @Override
    public void delPaymentAccountConfig(Long id) {
        merchantPaymentAccountConfigMapper.deleteById(id);
    }

    @Override
    public MerchantPaymentAccountConfigDO queryPaymentAccountConfigByCode(String code) {
        return merchantPaymentAccountConfigMapper.selectOne(MerchantPaymentAccountConfigDO::getCode,code);
    }

    @Override
    public PageResult<MerchantPaymentAccountConfigDO> queryConfigPage(MerchantPaymentAccountConfigPageQueryDto accountConfigPageQueryDto) {
        return merchantPaymentAccountConfigMapper.selectPage(accountConfigPageQueryDto, buildPaymentAccountWrapper(accountConfigPageQueryDto));
    }

    @Override
    public List<MerchantPaymentAccountConfigDO> queryConfigList(MerchantPaymentAccountConfigPageQueryDto accountConfigPageQueryDto) {
        return merchantPaymentAccountConfigMapper.selectList(buildPaymentAccountWrapper(accountConfigPageQueryDto));
    }

    @Override
    public void addMerchantReportConfig(MerchantPaymentConfigMappingDO merchantPaymentConfigMappingDO) {
        merchantPaymentConfigMappingMapper.insert(merchantPaymentConfigMappingDO);
    }

    @Override
    public void updateMerchantReportConfig(MerchantPaymentConfigMappingDO merchantPaymentConfigMappingDO) {
        merchantPaymentConfigMappingMapper.updateById(merchantPaymentConfigMappingDO);
    }

    @Override
    public void delMerchantReportConfig(Long id) {
        merchantPaymentConfigMappingMapper.deleteById(id);
    }

    @Override
    public MerchantPaymentConfigMappingDO queryConfigMapping(String relationId, Integer relationType, String accountCode) {
        MerchantPaymentConfigMappingPageQueryDto configPageQueryDto = new MerchantPaymentConfigMappingPageQueryDto();
        configPageQueryDto.setRelationId(relationId);
        configPageQueryDto.setRelationType(relationType);
        configPageQueryDto.setAccountCode(accountCode);
        LambdaQueryWrapperX<MerchantPaymentConfigMappingDO> queryWrapperX = buildPaymentMappingWrapper(configPageQueryDto);
        queryWrapperX.last("limit 1");
        return merchantPaymentConfigMappingMapper.selectOne(queryWrapperX);
    }

    @Override
    public PageResult<MerchantPaymentConfigMappingDto> queryConfigMappingPage(MerchantPaymentConfigMappingPageQueryDto mappingPageQueryDto) {
        IPage<MerchantPaymentConfigMappingDto> mpPage = MyBatisUtils.buildPage(mappingPageQueryDto);
        QueryWrapper<MerchantPaymentConfigMappingDto> queryWrapper = buildPaymentMappingWrapperDto(mappingPageQueryDto);
        merchantPaymentConfigMappingMapper.selectPageDto(mpPage, queryWrapper);
        return new PageResult(mpPage.getRecords(), mpPage.getTotal(), mpPage.getCurrent(), mpPage.getSize(), mpPage.getPages());
    }

    @Override
    public List<MerchantPaymentConfigMappingDto> queryConfigMappingList(String relationId, Integer relationType) {
        MerchantPaymentConfigMappingPageQueryDto queryDto= new MerchantPaymentConfigMappingPageQueryDto();
        queryDto.setRelationId(relationId);
        queryDto.setRelationType(relationType);
        QueryWrapper<MerchantPaymentConfigMappingDto> queryWrapper = buildPaymentMappingWrapperDto(queryDto);
        return merchantPaymentConfigMappingMapper.selectPageDto(queryWrapper);
    }

    /**
     * 查询条件封装
     *
     * @param merchantAppQueryDto
     * @return
     */
    LambdaQueryWrapperX<MerchantPaymentAccountConfigDO> buildPaymentAccountWrapper(MerchantPaymentAccountConfigPageQueryDto merchantAppQueryDto) {
        filterSn(merchantAppQueryDto);
        LambdaQueryWrapperX<MerchantPaymentAccountConfigDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(MerchantPaymentAccountConfigDO::getEntryType, merchantAppQueryDto.getEntryType());
        queryWrapper.eqIfPresent(MerchantPaymentAccountConfigDO::getAccountType, merchantAppQueryDto.getAccountType());
        queryWrapper.eqIfPresent(MerchantPaymentAccountConfigDO::getName, merchantAppQueryDto.getName());
        queryWrapper.eqIfPresent(MerchantPaymentAccountConfigDO::getCardNo, merchantAppQueryDto.getCardNo());
        queryWrapper.likeIfPresent(MerchantPaymentAccountConfigDO::getName, merchantAppQueryDto.getLikeName());
        queryWrapper.likeIfPresent(MerchantPaymentAccountConfigDO::getCardNo, merchantAppQueryDto.getLikeCardNo());

        queryWrapper.geIfPresent(MerchantPaymentAccountConfigDO::getCreateTime, merchantAppQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent(MerchantPaymentAccountConfigDO::getCreateTime, merchantAppQueryDto.getEndQueryTime());

        return queryWrapper;
    }

    /**
     * 查询条件封装
     *
     * @param merchantAppQueryDto
     * @return
     */
    LambdaQueryWrapperX<MerchantPaymentConfigMappingDO> buildPaymentMappingWrapper(MerchantPaymentConfigMappingPageQueryDto merchantAppQueryDto) {
        // filterSn(merchantAppQueryDto);
        LambdaQueryWrapperX<MerchantPaymentConfigMappingDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(MerchantPaymentConfigMappingDO::getRelationId, merchantAppQueryDto.getRelationId());
        queryWrapper.eqIfPresent(MerchantPaymentConfigMappingDO::getRelationType, merchantAppQueryDto.getRelationType());
        queryWrapper.eqIfPresent(MerchantPaymentConfigMappingDO::getAccountCode, merchantAppQueryDto.getAccountCode());
        return queryWrapper;
    }

    /**
     * 查询条件封装
     *
     * @param merchantAppQueryDto
     * @return
     */
    QueryWrapperX<MerchantPaymentConfigMappingDto> buildPaymentMappingWrapperDto(MerchantPaymentConfigMappingPageQueryDto merchantAppQueryDto) {
        // filterSn(merchantAppQueryDto);
        QueryWrapperX<MerchantPaymentConfigMappingDto> queryWrapper = new QueryWrapperX<>();
        queryWrapper.eqIfPresent("a.delete_flag", CommonEnum.YES_NO_TYPE.NO.getCode());
        queryWrapper.eqIfPresent("a.relation_id", merchantAppQueryDto.getRelationId());
        queryWrapper.eqIfPresent("a.relation_type", merchantAppQueryDto.getRelationType());
        queryWrapper.eqIfPresent("a.account_code", merchantAppQueryDto.getAccountCode());
        queryWrapper.eqIfPresent("b.sn", merchantAppQueryDto.getSn());
        return queryWrapper;
    }

}
